package im.xz.cn.xiacore.menu.mainMenu;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import im.xz.cn.xiacore.XiaCore;
import im.xz.cn.xiacore.menu.playerInfoMenu.playerMenu;
import im.xz.cn.xiacore.menu.shopMenu.shopMenu;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
public class menuEvent implements Listener {
    @EventHandler
    public void qwq(InventoryClickEvent event) {
        try {
            if (event.getView().getTitle().equalsIgnoreCase(ChatColor.LIGHT_PURPLE + "繁星梦服务器菜单")) {
                event.setCancelled(true);
                try {
                    if (event.getCurrentItem().getType().equals(Material.REDSTONE)) {
                        event.getWhoClicked().closeInventory();
                        try {
                            if (event.getWhoClicked().getWorld().getName().equals("lobby")) {
                                event.getWhoClicked().sendMessage(ChatColor.RED + "此功能在此被禁用");
                            }else {
                                Player player = (Player) event.getWhoClicked();
                                player.setHealth(0);
                                for (Object o : player.getServer().getOnlinePlayers().toArray()) {
                                    Player qwq = (Player) o;
                                    qwq.sendMessage(ChatColor.GRAY + player.getName() + " 选择了死亡");
                                }
                            }
                        }catch (NullPointerException ignore) {
                        }
                    }else if (event.getCurrentItem().getType().equals(Material.WATER_BUCKET)) {
                        if (!event.getWhoClicked().getWorld().getName().equalsIgnoreCase("lobby")) {
                            Player player = (Player) event.getWhoClicked();
                            player.closeInventory();
                            boolean weather = player.getWorld().isClearWeather();
                            String name = player.getName();
                            for (Object o :player.getServer().getOnlinePlayers().toArray()) {
                                Player qwq = (Player) o;
                                qwq.sendMessage(ChatColor.GRAY + player.getName() + " 修改了天气");
                            }
                            try {
                                if (weather) {
                                    player.getWorld().setStorm(true);
                                } else {
                                    player.getWorld().setStorm(false);
                                }
                            } catch (NullPointerException ignore) {
                            }
                        }else {
                            event.getWhoClicked().sendMessage(ChatColor.RED + "此功能在此被禁用");
                            event.getWhoClicked().closeInventory();
                        }
                    }else if (event.getCurrentItem().getType().equals(Material.SUNFLOWER)) {
                        Player player = (Player) event.getWhoClicked();
                        player.closeInventory();
                        String name = player.getName();
                        player.getWorld().setTime(1000);
                        for (Object o :player.getServer().getOnlinePlayers().toArray()) {
                            Player qwq = (Player) o;
                            qwq.sendMessage(ChatColor.GRAY + player.getName() + " 修改时间为白昼");
                        }
                    }else if (event.getCurrentItem().getType().equals(Material.DIAMOND)) {
                        Player player = (Player) event.getWhoClicked();
                        player.performCommand("tpsbar");
                        player.performCommand("rambar");
                        player.closeInventory();
                    }else if (event.getCurrentItem().getType().equals(Material.LIGHT_BLUE_BED)) {
                        Player player = (Player) event.getWhoClicked();
                        String name = player.getName();
                        player.closeInventory();
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("ConnectOther");
                        out.writeUTF(name);
                        out.writeUTF("bedwar");
                        player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                    }else if (event.getCurrentItem().getType().equals(Material.ZOMBIE_HEAD)) {
                        Player player = (Player) event.getWhoClicked();
                        String name = player.getName();
                        player.closeInventory();
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("ConnectOther");
                        out.writeUTF(name);
                        out.writeUTF("zombies");
                        player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                    }else if (event.getCurrentItem().getType().equals(Material.PIG_SPAWN_EGG)) {
                        Player player = (Player) event.getWhoClicked();
                        String name = player.getName();
                        player.closeInventory();
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("ConnectOther");
                        out.writeUTF(name);
                        out.writeUTF("hideAndSeek");
                        player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                    }else if (event.getCurrentItem().getType().equals(Material.COMPASS)) {
                        Player player = (Player) event.getWhoClicked();
                        String name = player.getName();
                        player.closeInventory();
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("ConnectOther");
                        out.writeUTF(name);
                        out.writeUTF("lobby");
                        player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                    }else if (event.getCurrentItem().getType().equals(Material.GRASS_BLOCK)) {
                        try {
                            if (event.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "normal服务器")) {
                                Player player = (Player) event.getWhoClicked();
                                String name = player.getName();
                                player.closeInventory();
                                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                                out.writeUTF("ConnectOther");
                                out.writeUTF(name);
                                out.writeUTF("normal");
                                player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                            }else {
                                Player player = (Player) event.getWhoClicked();
                                player.closeInventory();
                                String name = player.getName();
                                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                                out.writeUTF("ConnectOther");
                                out.writeUTF(name);
                                out.writeUTF("starCloud");
                                player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                            }
                        }catch (NullPointerException ignore) {
                        }
                    }else if (event.getCurrentItem().getType().equals(Material.WHITE_BANNER) | event.getCurrentItem().getType().equals(Material.RED_BANNER)) {
                        event.getWhoClicked().closeInventory();
                        Player player = (Player) event.getWhoClicked();
                        player.performCommand("pvp");
                    }else if (event.getCurrentItem().getType().equals(Material.CORNFLOWER)) {
                        event.getWhoClicked().closeInventory();
                        Player player = (Player) event.getWhoClicked();
                        try {
                            if (!player.getWorld().getName().equals("lobby")) {
                                player.performCommand("rtp");
                            }else {
                                player.sendMessage("此功能在此服务器被禁用");
                            }
                        }catch (NullPointerException ignore) {

                        }
                    }else if (event.getCurrentItem().getType().equals(Material.ANVIL)) {
                        event.getWhoClicked().closeInventory();
                        Player player = (Player) event.getWhoClicked();
                        String name = player.getName();
                        player.closeInventory();
                        ByteArrayDataOutput out = ByteStreams.newDataOutput();
                        out.writeUTF("ConnectOther");
                        out.writeUTF(name);
                        out.writeUTF("mod");
                        player.sendPluginMessage(XiaCore.getInstance(), "BungeeCord", out.toByteArray());
                    }else if (event.getCurrentItem().getType().equals(Material.PLAYER_HEAD)) {
                        Player player = (Player) event.getWhoClicked();
                        player.closeInventory();
                        playerMenu.playerMenu_qwq(player);
                    }else if (event.getCurrentItem().getType().equals(Material.GOLD_INGOT)) {
                        Player player = (Player) event.getWhoClicked();
                        player.closeInventory();
                        shopMenu.openShopMenu(player);
                    }
                }catch (NullPointerException ignore) {
                }
            }else {
            }
        }catch (NullPointerException ignore) {
        }
    }

}